package br.com.fogas.medicao.activity.helper;

import java.sql.SQLException;
import java.util.List;

import br.com.fogas.database.FogasDatabase;
import br.com.fogas.database.entidade.Cliente;
import br.com.fogas.database.entidade.PlanoVisitas;
import br.com.fogas.medicao.R;
import br.com.fogas.medicao.adapter.PlanoClientesAdapter;
import br.com.fogas.medicao.utils.MedicaoUtils;
import br.com.fogas.webservice.service.FogasWebservice;
import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnItemLongClick;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.widget.ListView;
import android.widget.TextView;

public class PlanoClientesHelper {
	
	private Fragment fragment;
	
	private FogasDatabase database;
	
	private FogasWebservice webservice;
	
	private List<PlanoVisitas> planos;
	
	private PlanoClientesAdapter planoAdapter;
	
	private PlanoVisitas planoSelecionado;
	
	private Handler handler;
	
	@InjectView(R.id.lvPlanoLojas) ListView lvPlanoLojas;

	@InjectView(R.id.tvPlanoLojaShoppingDesc) TextView tvPlanoLojaShoppingDesc;
	
	public PlanoClientesHelper(Fragment fragmentParent, PlanoVisitas planoLocal) {
		ButterKnife.inject(this, fragmentParent.getView());
		fragment = fragmentParent;
		database = new FogasDatabase(fragment.getActivity());
		webservice = new FogasWebservice(fragment.getActivity());
		handler = new Handler();
		
		try {	
			this.planos = database.getPlanoVisitas().listarPlanoClientesPorLocal(planoLocal.getCliente());
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		planoAdapter = new PlanoClientesAdapter(fragment.getActivity(), planos);
		lvPlanoLojas.setAdapter(planoAdapter);
		 
		tvPlanoLojaShoppingDesc.setText(MedicaoUtils.nomeCliente(planoLocal.getCliente()));
	}
	
	public void onDestroy() {
		database.fecharConexao();
		ButterKnife.reset(this); 
	}
	
	public void atualizarTela(final Cliente local) {
		handler.post(new Runnable() {
			public void run() {
				try {
					List<PlanoVisitas> planos = database.getPlanoVisitas().listarPlanoClientesPorLocal(local);
					PlanoClientesHelper.this.planos.clear();
					PlanoClientesHelper.this.planos.addAll(planos);
					PlanoClientesHelper.this.planoAdapter.notifyDataSetChanged();
					PlanoClientesHelper.this.lvPlanoLojas.invalidateViews();
					PlanoClientesHelper.this.tvPlanoLojaShoppingDesc.setText(MedicaoUtils.nomeCliente(local));
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		});
	}
	
	@OnItemLongClick(R.id.lvPlanoLojas)
	public boolean clienteclickLongo(int position) {
		planoSelecionado = (PlanoVisitas) lvPlanoLojas.getItemAtPosition(position);
		return false;
	}
	
	public Fragment getFragment() {
		return fragment;
	}
	
	public FogasDatabase getDatabase() {
		return database;
	}
	
	public FogasWebservice getWebservice() {
		return webservice;
	}
	
	public PlanoClientesAdapter getPlanoAdapter() {
		return planoAdapter;
	}
	
	public List<PlanoVisitas> getPlanos() {
		return planos;
	}
	
	public ListView getLvPlanoLojas() {
		return lvPlanoLojas;
	}
	
	public TextView getTvPlanoLojaShoppingDesc() {
		return tvPlanoLojaShoppingDesc;
	}
	
	public PlanoVisitas getPlanoSelecionado() {
		return planoSelecionado;
	}
}